package chapt05.ambient;
// Ambient.java
// OpenGL SuperBible
// Beginning of OpenGL lighting sample
// Demonstrates Ambient Lighting
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;

public class Ambient
{
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// This function does any needed initialization on the rendering context.
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Light values
		// Bright white light
		float ambientLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
		
		gl2.glEnable(GL2.GL_DEPTH_TEST); // Hidden surface removal
		gl2.glEnable(GL2.GL_CULL_FACE);  // Do not calculate inside of jet
		gl2.glFrontFace(GL2.GL_CCW);     // Counter clock-wise polygons face out
		
		// Lighting stuff
		gl2.glEnable(GL2.GL_LIGHTING); // Enable lighting
		
		// Set light model to use ambient light specified by ambientLight
		gl2.glLightModelfv(GL2.GL_LIGHT_MODEL_AMBIENT, ambientLight, 0);
		
		gl2.glEnable(GL2.GL_COLOR_MATERIAL); // Enable Material color tracking
		
		// Front material ambient and diffuse color track glColor
		gl2.glColorMaterial(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE);
		
		// Nice light blue
		gl2.glClearColor(0.0f, 0.0f, 5.0f, 1.0f);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float nRange = 80.0f;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		if(w <= h)
			gl2.glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange);
		else
			gl2.glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		
		// Save the matrix state
		gl2.glPushMatrix();
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		// Nose Cone /////////////////////////////
		// Bright Green
		gl2.glColor3ub((byte)0, (byte)255, (byte)0);
		gl2.glBegin(GL2.GL_TRIANGLES);
			gl2.glVertex3f(0.0f, 0.0f, 60.0f);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(0.0f, 15.0f, 30.0f);
			gl2.glVertex3f(0.0f, 0.0f, 60.0f);
			
			gl2.glVertex3f(0.0f, 0.0f, 60.0f);
			gl2.glVertex3f(0.0f, 15.0f, 30.0f);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			
			// Body of the Plane ////////////////////////
			// light gray
			gl2.glColor3ub((byte)192, (byte)192, (byte)192);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(0.0f, 15.0f, 30.0f);
			gl2.glVertex3f(0.0f, 0.0f, -56.0f);
			
			gl2.glVertex3f(0.0f, 0.0f, -56.0f);
			gl2.glVertex3f(0.0f, 15.0f, 30.0f);
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			
			gl2.glVertex3f(15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(-15.0f, 0.0f, 30.0f);
			gl2.glVertex3f(0.0f, 0.0f, -56.0f);
			
			///////////////////////////////////////////////
			// Left wing
			// Dark gray
			gl2.glColor3ub((byte)64, (byte)64, (byte)64);
			gl2.glVertex3f(0.0f, 2.0f, 27.0f);
			gl2.glVertex3f(-60.0f, 2.0f, -8.0f);
			gl2.glVertex3f(60.0f, 2.0f, -8.0f);
			
			gl2.glVertex3f(60.0f, 2.0f, -8.0f);
			gl2.glVertex3f(0.0f, 7.0f, -8.0f);
			gl2.glVertex3f(0.0f, 2.0f, 27.0f);
			
			gl2.glVertex3f(60.0f, 2.0f, -8.0f);
			gl2.glVertex3f(-60.0f, 2.0f, -8.0f);
			gl2.glVertex3f(0.0f, 7.0f, -8.0f);
			
			// Other wing top section
			gl2.glVertex3f(0.0f, 2.0f, 27.0f);
			gl2.glVertex3f(0.0f, 7.0f, -8.0f);
			gl2.glVertex3f(-60.0f, 2.0f, -8.0f);
			
			// Tail section ///////////////////////////////
			// Bottom of back fin
			gl2.glColor3ub((byte)255, (byte)255, (byte)0);
			gl2.glVertex3f(-30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(0.0f, -0.5f, -40.0f);
			
			// top of left side
			gl2.glVertex3f(0.0f, -0.5f, -40.0f);
			gl2.glVertex3f(30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(0.0f, 4.0f, -57.0f);
			
			// top of right side
			gl2.glVertex3f(0.0f, 4.0f, -57.0f);
			gl2.glVertex3f(-30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(0.0f, -0.5f, -40.0f);
			
			// back of bottom of tail
			gl2.glVertex3f(30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(-30.0f, -0.5f, -57.0f);
			gl2.glVertex3f(0.0f, 4.0f, -57.0f);
			
			// Top of Tail section left
			gl2.glColor3ub((byte)255, (byte)0, (byte)0);
			gl2.glVertex3f(0.0f, 0.5f, -40.0f);
			gl2.glVertex3f(3.0f, 0.5f, -57.0f);
			gl2.glVertex3f(0.0f, 25.0f, -65.0f);
			
			gl2.glVertex3f(0.0f, 25.0f, -65.0f);
			gl2.glVertex3f(-3.0f, 0.5f, -57.0f);
			gl2.glVertex3f(0.0f, 0.5f, -40.0f);
			
			// Back of horizontal section
			gl2.glVertex3f(3.0f, 0.5f, -57.0f);
			gl2.glVertex3f(-3.0f, 0.5f, -57.0f);
			gl2.glVertex3f(0.0f, 25.0f, -65.0f);
		gl2.glEnd();
		
		gl2.glPopMatrix();
		
		// Display the results
		gl2.glFlush();
	}
}